package com.gao.stonegame;

import com.gao.frame.Jbutton;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class MainFrame extends JFrame implements KeyListener {

    int[][] data = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 0},
            {13, 14, 15, 12}
    };
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    int row;
    int column;
    int count;

    public MainFrame() {
        //窗体对象.addKeyListener(KeyListener实现类对象)
        this.addKeyListener(this);
        //初始化窗体
        initFrame();
        initDtta();
        //绘制界面
        paintView();
        setVisible(true);
    }

    /*
        初始化数据
     */
    public void initDtta() {
        Random r = new Random();
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                int xRandom = r.nextInt(4);
                int yRandom = r.nextInt(4);
                int temp = data[i][j];
                data[i][j] = data[xRandom][yRandom];
                data[xRandom][yRandom] = temp;
            }
        }
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == 0) {
                    row = i;
                    column = j;
                }
            }
        }
    }

    public void initFrame() {
        setSize(514, 595);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("石头迷阵小游戏");
        setLayout(null);
        setAlwaysOnTop(true);
        setLocationRelativeTo(null);

    }

    /*
            此方法用于绘制界面
     */
    public void paintView() {
        super.getContentPane().removeAll();
        if (victory()) {
            JLabel winLable = new JLabel(new ImageIcon("D:\\新建文件夹\\MobileFile\\win.png"));
            winLable.setBounds(124, 230, 266, 88);
            getContentPane().add(winLable);
        }
        JLabel scoreLable = new JLabel("步数为："+count);
        scoreLable.setBounds(50,20,100,20);
        getContentPane().add(scoreLable);
        JButton btn = new JButton("重新游戏");
        btn.setBounds(350,20,100,20);
        getContentPane().add(btn);
        btn.setFocusable(false);
        btn.addActionListener(e -> {
            count = 0;
            initDtta();
            paintView();
        });
        for (int i = 0; i < 4; i++) {
            for (int z = 0; z < 4; z++) {
                JLabel jl1 = new JLabel(new ImageIcon("D:\\新建文件夹\\MobileFile\\" + data[i][z] + ".png"));
                jl1.setBounds(50 + 100 * z, 90 + 100 * i, 100, 100);
                getContentPane().add(jl1);
            }
        }
        JLabel background = new JLabel(new ImageIcon("D:\\新建文件夹\\MobileFile\\background.png"));
        background.setBounds(26, 30, 450, 484);
        getContentPane().add(background);
        super.getContentPane().repaint();
    }

    /*
            判断游戏是否胜利
     */
    public boolean victory() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        move(keyCode);
        paintView();
    }

    /*
            此方法用于移动
     */
    private void move(int keyCode) {
        if (victory()) {
            return;
        }

        if (keyCode == 37) {
            if (column == 3) {
                return;
            }
            //空白快与右方块进行交换
            int temp = data[row][column];
            data[row][column] = data[row][column + 1];
            data[row][column + 1] = temp;
            column++;
            count++;
        } else if (keyCode == 38) {
            if (row == 3) {
                return;
            }
            //空白块与下边的元素进行交换
            int temp = data[row][column];
            data[row][column] = data[row + 1][column];
            data[row + 1][column] = temp;
            row++;
            count++;
        } else if (keyCode == 39) {
            if (column == 0) {
                return;
            }
            //空白块与左边的元素进行交换
            int temp = data[row][column];
            data[row][column] = data[row][column - 1];
            data[row][column - 1] = temp;
            column--;
            count++;
        } else if (keyCode == 40) {
            if (row == 0) {
                return;
            }
            //空白块与上边的元素进行交换
            int temp = data[row][column];
            data[row][column] = data[row - 1][column];
            data[row - 1][column] = temp;
            row--;
            count++;
        } else if (keyCode == 90) {
            //触发作弊器
            data = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
